﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using System.Windows.Forms;
using fideeraSoft.Utils;
using fideeraSoft.Entities;

namespace fideeraSoft.Paneles
{
    public class MateriaPrimaPanelBaja:PanelGeneral
    {
        private System.Windows.Forms.Button btEliminar;
        private System.Windows.Forms.Button btBuscarMatPrima;
        private System.Windows.Forms.TableLayoutPanel tlpBuscarMatPrima;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.DataGridView dgvBaja;
        private System.Windows.Forms.TextBox tbBuscarMatPrima;
        private int posicionDataGrid;
        private int posicionTablaBotones;


        public MateriaPrimaPanelBaja(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            InitializeComponent();

            this.formatearTableTitulo("BAJA DE MATERIA PRIMA / INSUMO");

            
            this.formatearTablaTLP();
            this.formatearDataGrid();
            this.formatearTablaBotones();

            this.Controls.Add(tlpBuscarMatPrima);

            this.Controls.Add(dgvBaja);

            this.Controls.Add(tlpBotones);
        }

        #region FORMATEO TABLAS
        private void formatearTablaTLP()
        {
            tlpBuscarMatPrima.Controls.Add(btBuscarMatPrima);
            tlpBuscarMatPrima.Controls.Add(tbBuscarMatPrima);
                                    
            tlpBuscarMatPrima.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarMatPrima.Size.Width), this.InitialRealPointVertical);            
            
        }


        private void formatearDataGrid()
        {

            posicionDataGrid = this.InitialRealPointVertical + tlpBuscarMatPrima.Size.Height + 60;

            dgvBaja.AutoGenerateColumns = true;
            dgvBaja.Size = new Size(this.Size.Width - 100, 100);
            dgvBaja.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvBaja.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvBaja.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvBaja.BorderStyle = BorderStyle.Fixed3D;

            dgvBaja.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvBaja.Size.Width), posicionDataGrid);
        }

        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btEliminar);

            posicionTablaBotones = posicionDataGrid + dgvBaja.Size.Height + 40;
                        
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionTablaBotones);

        }
        #endregion

        #region INICIALIZACION
        private void InitializeComponent()
        {
            this.btEliminar = new System.Windows.Forms.Button();
            this.btBuscarMatPrima = new System.Windows.Forms.Button();
            this.tlpBuscarMatPrima = new System.Windows.Forms.TableLayoutPanel();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.dgvBaja = new System.Windows.Forms.DataGridView();
            this.tbBuscarMatPrima = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgvBaja)).BeginInit();
            this.SuspendLayout();
            // 
            // btEliminar
            // 
            this.btEliminar.BackColor = System.Drawing.Color.AliceBlue;
            this.btEliminar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btEliminar.Location = new System.Drawing.Point(0, 0);
            this.btEliminar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btEliminar.Name = "btEliminar";
            this.btEliminar.Size = new System.Drawing.Size(75, 40);
            this.btEliminar.TabIndex = 0;
            this.btEliminar.Text = "Eliminar";
            this.btEliminar.UseVisualStyleBackColor = false;
            this.btEliminar.Click += new System.EventHandler(this.btEliminar_Click);
            // 
            // btBuscarMatPrima
            // 
            this.btBuscarMatPrima.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarMatPrima.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarMatPrima.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarMatPrima.Location = new System.Drawing.Point(0, 0);
            this.btBuscarMatPrima.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btBuscarMatPrima.Name = "btBuscarMatPrima";
            this.btBuscarMatPrima.Size = new System.Drawing.Size(75, 23);
            this.btBuscarMatPrima.TabIndex = 0;
            this.btBuscarMatPrima.Text = "Buscar Materia Prima (Nombre)";
            this.btBuscarMatPrima.UseVisualStyleBackColor = false;
            this.btBuscarMatPrima.Click += new System.EventHandler(this.btBuscarMatPrima_Click);
            // 
            // tlpBuscarMatPrima
            // 
            this.tlpBuscarMatPrima.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMatPrima.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMatPrima.ColumnCount = 2;
            this.tlpBuscarMatPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrima.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrima.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMatPrima.Name = "tlpBuscarMatPrima";
            this.tlpBuscarMatPrima.RowCount = 1;
            this.tlpBuscarMatPrima.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMatPrima.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarMatPrima.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Cursor = System.Windows.Forms.Cursors.Default;
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Name = "btVolver";
            this.btVolver.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btVolver.Size = new System.Drawing.Size(85, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // dgvBaja
            // 
            this.dgvBaja.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvBaja.Location = new System.Drawing.Point(0, 0);
            this.dgvBaja.Name = "dgvBaja";
            this.dgvBaja.ReadOnly = true;
            this.dgvBaja.Size = new System.Drawing.Size(240, 150);
            this.dgvBaja.TabIndex = 0;
            this.dgvBaja.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvBaja_CellClick);
            // 
            // tbBuscarMatPrima
            // 
            this.tbBuscarMatPrima.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarMatPrima.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarMatPrima.MaxLength = 50;
            this.tbBuscarMatPrima.Name = "tbBuscarMatPrima";
            this.tbBuscarMatPrima.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarMatPrima.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvBaja)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        private void btBuscarMatPrima_Click(object sender, EventArgs e)
        {
            
            
            if (this.dgvBaja.DataSource != null)
            {
                this.dgvBaja.DataSource = null;
            }
            else
            {
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                dgvBaja.ClearSelection();
                this.dgvBaja.Rows.Clear();
            }
            dgvBaja.Refresh();

            if (!string.IsNullOrEmpty(tbBuscarMatPrima.Text))
            {
                try
                {
                    MateriaPrima matPrima= handler.buscarMateriaPrimaPorNombre(tbBuscarMatPrima.Text);
                    if (matPrima!= null)
                    {
                        List<MateriaPrima> lstProveedor = new List<MateriaPrima>();
                        lstProveedor.Add(matPrima);

                        dgvBaja.DataSource = lstProveedor;
                        //   MessageBox.Show("CARGA DE PROVEEDOR ENCONTRADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //DEBUG
                        dgvBaja.EndEdit();
                        dgvBaja.Refresh();

                    }
                    else
                    {
                        MessageBox.Show("NO SE ENCONTRO LA MATERIA PRIMA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (MiException exc)
                {
                    //MessageBox.Show(exc.Message);     //DEBUG
                    CLogger.Append("[btBuscarMatPrima_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
                tbBuscarMatPrima.Clear();

            }
            else
            {
                MessageBox.Show("NO SE INGRESO NINGÚN NOMBRE DE MATERIA PRIMA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        
        }

        private void btEliminar_Click(object sender, EventArgs e)
        {
            try
            {
                if ((dgvBaja != null) && (dgvBaja.Rows.Count != 0))
                {
                    int indx = dgvBaja.CurrentCell.RowIndex;

                    string strDataId = dgvBaja[0, indx].Value.ToString();
                    string strDataName = dgvBaja[1, indx].Value.ToString();

                    DialogResult res = MessageBox.Show("Seguro quiere eliminar la Materia Prima '" + strDataName + "' - ID: " + strDataId, "ERROR", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res == DialogResult.Yes)
                    {
                        this.handler.eliminarMateriaPrimaPorID(Convert.ToInt32(strDataId));
                        MessageBox.Show("Materia Prima eliminada correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        tbBuscarMatPrima.Clear();                        

                        if (this.dgvBaja.DataSource != null)
                        {
                            this.dgvBaja.DataSource = null;
                        }
                        else
                        {
                            //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                            //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                            this.dgvBaja.Rows.Clear();
                        }
                        dgvBaja.Refresh();
                    }
                    else
                    {

                    }
                }
                else
                {
                    MessageBox.Show("No hay ninguna Materia Prima en la lista", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (MiException exc)
            {
                MessageBox.Show("No se pudo eliminar la Materia Prima", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                CLogger.Append("[btEliminar_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
        }

        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void dgvBaja_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvBaja.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }
    }
}
